<template>
  <el-dialog title="编辑员工" :visible="show" @close="closeEvent">
    <el-form
      label-width="150px"
      width="600px"
      :model="form"
      :rules="rules"
      ref="form"
    >
      <el-form-item label="姓名" prop="username"
        ><el-input placeholder="1-30个字符" v-model="form.username"></el-input
      ></el-form-item>
      <el-form-item label="手机" prop="mobile"
        ><el-input placeholder="请输入手机号" v-model="form.mobile"></el-input
      ></el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry"
        ><el-date-picker
          type="date"
          placeholder="请选择日期"
          v-model="form.timeOfEntry"
        ></el-date-picker
      ></el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment"
        ><el-select v-model="form.formOfEmployment"
          ><el-option
            v-for="item in formate"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option></el-select
      ></el-form-item>
      <el-form-item label="工号" prop="workNumber"
        ><el-input placeholder="1-20个字符" v-model="form.workNumber"></el-input
      ></el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <div ref="treeArea">
          <el-input
            readonly
            placeholder="请选择"
            v-model="form.departmentName"
            @focus="focusEvent"
          ></el-input>
          <el-tree
            v-if="showTree"
            :data="treeData"
            @node-click="nodeClick"
            :props="{ label: 'name' }"
          ></el-tree>
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime"
        ><el-date-picker
          type="date"
          placeholder="请选择日期"
          v-model="form.correctionTime"
        ></el-date-picker
      ></el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align:center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import formaterList from '@/assets/constant/employees.js'
import { companyDepartment } from '@/api/departaments.js'
import { sysUserPost } from '@/api/employees.js'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      showTree: false, // 控制树形组件的显示
      treeData: [], // 树形组织架构数据
      formate: formaterList.hireType, // 聘用形式
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [{ required: true, message: '必填', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  mounted () {
    // 监听点击事件
    document.addEventListener('click', this.clickEvent)
  },
  beforeDestroy () {
    document.removeEventListener('click', this.clickEvent)
  },
  methods: {
    // 关闭弹框
    closeEvent () {
      this.$emit('update:show', false)
    },
    // 提交
    submit () {
      this.$refs.form.validate(async resulte => {
        if (resulte) {
          await sysUserPost(this.form)
          this.$message.success('新增成功')
          this.closeEvent()
          this.$emit('handleCurrentChange', 1)
        }
      })
    },
    // 输入框触发事件，获取组织架构数据
    async focusEvent () {
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
      }
    },
    // 树形数据转换
    changeData (arr, str = '') {
      return arr.filter(item => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 树形组件内点击事件
    nodeClick (data) {
      this.form.departmentName = data.name
    },
    // 隐藏树形组件
    clickEvent (e) {
      // 判断点击的位置是否在树形区域内
      if (this.$refs.treeArea && !this.$refs.treeArea.contains(e.target)) {
        this.showTree = false
      }
    }
  }
}
</script>

<style></style>
